﻿using System.Collections.Generic;
using UnityEngine;

namespace HBattle
{
    public class EntityManager : SingletonManager<EntityManager>
    {
        private Dictionary<int, BaseGameEntity> EntityMap;

        public EntityManager() { }

        public void RegisterEntity(BaseGameEntity newEntity)
        {
            EntityMap.Add(newEntity.ID, newEntity);
        }

        public BaseGameEntity GetEntityFromID(int id)
        {
            return EntityMap[id];
        }

        public void RemoveEntity(BaseGameEntity entity)
        {
            EntityMap.Remove(entity.ID);
        }


        public void Start()
        {
            EntityMap = new Dictionary<int, BaseGameEntity>();

            // todo Test1
            CreateTest();
        }


        public void Update()
        {
            if (EntityMap == null)
            {
                return;
            }

            foreach(var it in EntityMap)
            {
                it.Value.Update(Time.deltaTime);
            }
        }

        public void Destroy()
        {
            EntityMap.Clear();
            EntityMap = null;
        }



        // todo CreateTest
        public void CreateTest()
        {
            Vehicle vehicle1 = new Vehicle(1);
            Vehicle vehicle2 = new Vehicle(2);
            vehicle2.Position = new Vector3(30, 0, 0);

            EntityManager.Instance.RegisterEntity(vehicle1);
            EntityManager.Instance.RegisterEntity(vehicle2);
        }
    }
}

